package com.softplus.carrefour;

import java.io.PrintStream;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

/**
 *Carrefour Exception 
 */
public class CFException extends Exception {

    private static final String BUNDLE_NAME = 
        "com.softplus.carrefour.CFException"; //$NON-NLS-1$
    private static final ResourceBundle RESOURCE_BUNDLE = 
        ResourceBundle.getBundle(BUNDLE_NAME);
    private ResourceBundle resourceBundle = null;
    private Exception e = null;
    protected String id = null;
    private ArrayList message = null;
    private String errorMessage = null;


    /**
     *Throws Exception normally
     */
    public CFException() {
    }

    /**
     * Throws Exception with id 
     * @param java.lang.String id
     */
    public CFException(String id) {
        this.id = id;
    }

    /**
     * Throws Exception with other Exception
     * @param Exception e
     */
    public CFException(Exception e) {
        this.e = e;
    }


    /**
     * Throws Exception with id and other Exception
     * @param java.lang.String id
     * @param Exception e
     */
    public CFException(String id, Exception e) {
        this.e = e;
        this.id = id;
    }


    /**
     * Throws Exception with id and other Message
     * @param java.lang.String id
     * @param java.lang.String message1
     */
    public CFException(String id, String message1) {
        this(id);
        message = new ArrayList();
        message.add(message1);
    }

    /**
     * Throws Exception with id and other Message
     * @param java.lang.String id
     * @param java.lang.String message1
     * @param java.lang.String message2
     */
    public CFException(String id, String message1, String message2) {
        this(id, message1);
        message.add(message2);
    }

    /**
     * Throws Exception with id and other Message
     * @param java.lang.String id
     * @param java.lang.String message1
     * @param java.lang.String message2
     * @param java.lang.String message3
     */
    public CFException(String id, String message1, String message2, 
                       String message3) {
        this(id, message1, message2);
        message.add(message3);
    }

    /**
     * Get Error message from ID
     * @return String : error Message
     */
    public String getMessageFromID() {
        return (this.id != null) ? RESOURCE_BUNDLE.getString(id) : null;
    }

    /**
     * Get Error message from ID With Argument
     * @return String : error Message
     */
    public String getMessageFromIDWithArg() {
        String str = null;
        StringBuffer sb = new StringBuffer();
        if (this.id != null) {
            str = RESOURCE_BUNDLE.getString(id);
            if (message != null)
                for (int i = 0; i < message.size(); i++) {
                    if (i < message.size() - 1) {
                        sb.append(message.get(i) + "\n");
                    } else {
                        sb.append(message.get(i));
                    }
                }
        }
        return sb.toString();
    }

    /**
     * Get Description of Error
     * @return java.lang.String
     */
    public String getDescription() {
        return getDescription(null);
    }

    /**
     * Get Description of Error with local
     * @param java.util.Locale locale
     * @return String : Description of Error
     */
    public String getDescription(Locale locale) {
        if (locale == null) {
            locale = Locale.getDefault();
        }

        if (id != null) {
            resourceBundle = 
                    java.util.ResourceBundle.getBundle(this.getClass().getName(), 
                                                       locale);
            String description = "";

            try {
                StringTokenizer stk = 
                    new StringTokenizer(resourceBundle.getString(id), "|");
                stk.nextToken();
                description = stk.nextToken();

            } catch (MissingResourceException mre) {

                description = 
                        "This Error is not defined in properties file :" + 
                        mre.getMessage();
            }

            return description;

        } else if (e != null) {
            return e.getMessage();
        } else {
            return "";
        }
    }

    /**
     * Print Stacktrace
     */
    public void printStackTrace() {
        System.out.println("-----------  Standard Exception : printStackTrace ------------");

        if (e != null) {
            e.printStackTrace();
        }

        if (id != null) {
            System.out.println("Error Code = " + id);
            System.out.println(RESOURCE_BUNDLE.getString(id));
        }

        if (errorMessage != null) {
            System.out.println("Error Message = " + errorMessage);
        }

        if (message != null) {
            for (int i = 0; i < message.size(); i++)
                System.out.println("Message = " + message.get(i));
        }

        System.out.println("-----------  End Standard Exception : printStackTrace ------------");
    }

    /**
     * Print Stacktrace with PrintStream
     * @param java.io.PrintStream out
     */
    public void printStackTrace(PrintStream out) {
        if (e != null) {
            e.printStackTrace(out);
        }
    }

    /**
     * Print Stacktrace with PrintWriter
     * @param java.io.PrintWriter out
     */
    public void printStackTrace(PrintWriter out) {
        if (e != null) {
            e.printStackTrace(out);
        }
    }


}
